package com.arrake.utils;

import org.dom4j.Document;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NormalOperation {
    //    全局格式化Text
    public static String globalFormatText(String docxText) {
        //
        docxText = docxText.replaceAll("．", ".");
        //
        while (docxText.contains("\t\t")) {
            docxText = docxText.replaceAll("\t\t", "\t");
        }
        //

        return docxText;
    }

    //    全局格式化Xml
    public static void globalFormatXml(Document docXml, String filePath) throws Exception {
        //替换图片
        FormatToReplace.picToLabel(docXml);
        //替换tab
        FormatToReplace.tabTo(docXml);
        //替换着重号
        FormatToReplace.markEmphasis(docXml);
        //替换居中格式
        FormatToReplace.textCenter(docXml);
        //替换数字序列号
        FormatToReplace.serialNumberTo(docXml, filePath);
        //替换字母序列号
        FormatToReplace.upperTO(docXml, filePath);
        //替换下划线
        FormatToReplace.underlineTo(docXml);
        //替换上标
        FormatToReplace.superscript(docXml);
        //替换右对齐
        FormatToReplace.textRight(docXml);
    }

    //题型分类
    public static List<String> division(String regex, String docxText) {

        Matcher matcher = Pattern.compile(regex).matcher(docxText);
        List<String> bigBlockList = new ArrayList<>();
        while (matcher.find()) {
            //分开模块
            String[] split01 = docxText.split(matcher.group(1).replace("(", "\\(").replace(")", "\\)"), 2);
            bigBlockList.add(split01[0]);
            docxText = split01[1];
        }
        bigBlockList.add(docxText);
        return bigBlockList;
    }

    //    单选题处理
    public static String singleChooseChinese(String singeText) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.SINGLE.123&gt;单项选择题\n\n";
        //提取每道题目
        Matcher matcher1 = Pattern.compile("\\d{1,2}\\.[\\s\\S]*?D\\..*\\n").matcher(singeText);
        singeText = "";

        while (matcher1.find()) {
            //题干与选项分离
            //题目
            String topic = matcher1.group();
            String title = null;
            String option = null;
            String sufOption = "【答案】A\n" +
                    "【解析】这里是单选题的单选解析\n" +
                    "【分值】5"+"\n\n";
            Matcher matcher2 = Pattern.compile("(\\d{1,2}[\\s\\S]*)A").matcher(topic);
            while (matcher2.find()) {
                //题干
                title = matcher2.group(1);
            }
            option = topic.replace(title, "");

            //对题干格式化
            //
            title = title.replaceAll("\t| ", "");
            //对选项格式化
            //
            while (option.contains("\t\t")) {
                option = option.replaceAll("\t\t", "\t");
            }

            option = option.replaceAll("\t", "\n");
            //
            option = option.replaceAll(" ", "");
            //
            option = option.replace(".\n", ".");
            while (option.contains("\n\n")) {
                option = option.replaceAll("\n\n", "\n");
            }

            //合并
            topic = topic.replace("\n\n", "\n");
            topic = title + option + sufOption;

            singeText = singeText + topic;

        }
        singeText = pre + singeText;

        return singeText;
    }

    //    多选题处理
    public static String multipleChooseChinese(String multipleText) throws UnsupportedEncodingException {
        //匹配出选项有几个，是到H还是E
        String pre = "&lt;block&gt;\n" +
                "&lt;TYPE.MULTI.108&gt;多选题"+"\n\n";
        String var = "H";
        String regexE = "\\d{1,2}\\.[\\s\\S]*?" + var + "\\..*\\n";
        Matcher matcher1 = Pattern.compile(regexE).matcher(multipleText);
        while (!matcher1.find()) {
            byte[] bytes = var.getBytes();
            bytes[0]--;
            var = new String(bytes, "utf8");
            regexE = "\\d{1,2}\\.[\\s\\S]*?" + var + "\\..*\\n";
            matcher1 = Pattern.compile(regexE).matcher(multipleText);
        }

        multipleText = "";

        matcher1 = matcher1.reset();
        while (matcher1.find()) {
            //将题目与题干分开
            String topic = matcher1.group();
            String title = null;
            String option = null;
            String sufOption = "【答案】ABC\n" +
                    "【解析】这里是单选题的单选解析\n" +
                    "【分值】5"+"\n\n";
            Matcher matcher2 = Pattern.compile("(\\d{1,2}[\\s\\S]*)A.").matcher(topic);
            while (matcher2.find()) {
                //题干
                title = matcher2.group(1);
            }
            option = topic.replace(title, "");
            //对题干格式化
            //
            title = title.replaceAll("\t| ", "");
            //对选项格式化
            //
            while (option.contains("\t\t")) {
                option = option.replaceAll("\t\t", "\t");
            }
            option = option.replaceAll("\t", "\n");
            //
            option = option.replaceAll(" ", "");
            //
            option = option.replace(".\n", ".");
            //题目格式化
            topic = topic.replace("\n\n", "\n");
            //合并
            topic = title + option + sufOption;
            multipleText = multipleText + topic;
        }
        multipleText = pre + multipleText;
        return multipleText;
    }

    public static String singleChooseEnglish(String singeText) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.SINGLE.123&gt;单项选择题\n\n";
        //提取每道题目
        Matcher matcher1 = Pattern.compile("\\d{1,2}\\.[\\s\\S]*?D\\..*\\n").matcher(singeText);
        singeText = "";

        while (matcher1.find()) {
            //题干与选项分离
            //题目
            String topic = matcher1.group();
            String title = null;
            String option = null;
            String sufOption = "【答案】A\n" +
                    "【解析】这里是单选题的单选解析\n" +
                    "【分值】5"+"\n\n";
            Matcher matcher2 = Pattern.compile("(\\d{1,2}[\\s\\S]*)A\\.").matcher(topic);
            while (matcher2.find()) {
                //题干
                title = matcher2.group(1);
            }
            option = topic.replace(title, "");
            //对题干格式化
            //
            title = title.replaceAll("\t", "");
            //对选项格式化
            //
            while (option.contains("\t\t")) {
                option = option.replaceAll("\t\t", "\t");
            }
            option = option.replaceAll("\t", "\n");

            topic = topic.replace("\n\n", "\n");
            topic = topic.replace(". ", ".");
            //合并
            topic = title + option + sufOption;
            singeText = singeText + topic;

        }
        singeText = pre + singeText;

        return singeText;
    }

    //
    public static String multipleChooseEnglish(String multipleText) throws UnsupportedEncodingException {
        //匹配出选项有几个，是到H还是E
        String pre = "prefix\n";
        String var = "H";
        String regexE = "\\d{1,2}\\.[\\s\\S]*?" + var + "\\..*\\n";
        Matcher matcher1 = Pattern.compile(regexE).matcher(multipleText);
        while (!matcher1.find()) {
            byte[] bytes = var.getBytes();
            bytes[0]--;
            var = new String(bytes, "utf8");
            regexE = "\\d{1,2}\\.[\\s\\S]*?" + var + "\\..*\\n";
            matcher1 = Pattern.compile(regexE).matcher(multipleText);
        }

        multipleText = "";

        matcher1 = matcher1.reset();
        while (matcher1.find()) {
            //将题目与题干分开
            String topic = matcher1.group();
            String title = null;
            String option = null;
            String sufOption = "suffix\n";
            Matcher matcher2 = Pattern.compile("(\\d{1,2}[\\s\\S]*)A").matcher(topic);
            while (matcher2.find()) {
                //题干
                title = matcher2.group(1);
            }
            option = topic.replace(title, "");
            //对题干格式化
            //
            title = title.replaceAll("\t| ", "");
            //对选项格式化
            //
            while (option.contains("\t\t")) {
                option = option.replaceAll("\t\t", "\t");
            }
            option = option.replaceAll("\t", "\n");
            //
            option = option.replaceAll(" ", "");
            //题目格式化
            topic = topic.replace("\n\n", "\n");
            //合并
            topic = title + option + sufOption;
            multipleText = multipleText + topic;
        }
        multipleText = pre + multipleText;
        return multipleText;
    }

    public static String singleLineChinese(String translationText, String pre) {
        String suffix = "【答案】这是主观题题的答案\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        Matcher matcher1 = Pattern.compile("\\d{1,2}\\.[\\s\\S]*?\\n").matcher(translationText);
        translationText = "";
        while (matcher1.find()) {
            translationText = translationText + matcher1.group() + suffix;
            //格式化题目
            translationText = translationText.replace(".\t", ".");
        }
        translationText = pre + translationText;
        return translationText;
    }

    public static String singleLineChinesePlus(String text) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.SUBJECT.33&gt;综合题\n\n";
        String suffix = "【答案】这是主观题题的答案\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        int count = 0;
        //格式化文本
        while (text.contains("\n\n")) {
            text=text.replace("\n\n", "\n");
        }
        //先将图片分开
        Map<String, String> imgMap = new HashMap<>();
        Matcher matcher1 = Pattern.compile("&lt;img style=\".*?\" id=\".*?\"/&gt;").matcher(text);
        while (matcher1.find()) {
            count++;
            text = text.replace(matcher1.group(), "img_" + count);
            imgMap.put("img_" + count, matcher1.group());
        }

        //将分开每一行判断
        String[] split = text.split("\n");
        List<String> topics = new ArrayList<>();
        String topic = null;
        for (String s : split) {

            Matcher matcher = Pattern.compile("^\\d{1,2}\\.[\\s\\S]*").matcher(s);
            if (matcher.find()) {
                if (topic != null) {
                    topics.add(topic);
                }
                topic = "";
                topic = s;
            } else {
                topic = topic + "\n"+s;
            }
        }
        topics.add(topic);
        List<String> topicsTemp = new ArrayList<>();
        for (String s : topics) {
            Matcher matcher = Pattern.compile("img_\\d").matcher(s);
            while (matcher.find()) {
                //换回来
                s = s.replace(matcher.group(), imgMap.get(matcher.group()));
            }
            topicsTemp.add(s);
        }
        text = pre;
        for (String s : topicsTemp) {
            text = text + s + "\n"+suffix;
        }

        return text;
    }
    public static String readChinese(String readText, String pre, String pre_c) {

        String suffix = "【答案】这是主观题题的答案\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        List<String> block = new ArrayList<>();
        //将题目分开,j
        Matcher matcher1 = Pattern.compile("\\d{1,2}\\.([\\s\\S]*?\\n)").matcher(readText);

        while (matcher1.find()) {
            String preS = matcher1.group(1).replace("\t", "");
            String[] split = readText.split(matcher1.group(), 2);

            block.add(split[0]);
            readText = preS + split[1];

        }
        block.add(readText);

        block.remove(0);
        readText = "";
        //对题目处理
        for (String s1 : block) {
//                   先将题目与材料分离，将题目根据\n分割，然后判断：如果这一行包含特定格式的如（*）为（1），否为上一题的子题
            String material = null;
            Matcher matcher2 = Pattern.compile("([\\s\\S]*?)（[1一]）").matcher(s1);
            while (matcher2.find()) {
                material = matcher2.group(1);
                String[] split = s1.replace(material, "").split("\n");
                //对材料添加段落标签 ！
                material = FormatToReplace.paragraphTags(material);
                List<String> topics = new ArrayList<>();
                int count = -1;
                for (String s : split) {
                    count++;
                    if (s.matches("（\\d）.*")) {
                        topics.add(s);
                    } else {
                        topics.set(count - 1, topics.get(count - 1) + s);
                    }
                }
                s1 = "";
                for (String topic : topics) {
                    topic = topic + "\n" + suffix;
                    s1 = s1 + topic;
                }
            }
            s1 = pre + material + pre_c + s1;
            readText = readText + s1;
        }
        return readText;
    }

    public static String writeChinese(String writeText) {
        String pre = "&lt;block&gt;\n" +
                "&lt;TYPE.SUBJECT.29&gt;作文\n\n";
        String suffix = "【答案】这是主观题题的答案\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        Matcher matcher1 = Pattern.compile("\\d{1,2}([\\s\\S]*)").matcher(writeText);
        while (matcher1.find()) {
            // 对材料添加段落标签 ！
            writeText = matcher1.group(1).replaceAll("\\.|\\t", "");
            writeText = FormatToReplace.paragraphTags(writeText);
            writeText = pre + writeText + suffix;
        }
        return writeText;
    }

    public static String readingEnglish(String readingText) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.MATERIAL.21&gt;Reading Comprehension\n" +
                "&lt;材料&gt;";
        String pre_c = "\n&lt;TYPE.SINGLE.21&gt;\n\n";
        String suffix = "【答案】A\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        List<String> block = new ArrayList<>();
        //将题目分开
        Matcher matcher1 = Pattern.compile("Passage \\d\\n").matcher(readingText);

        while (matcher1.find()) {
            String[] split = readingText.split(matcher1.group(), 2);

            block.add(split[0]);
            readingText = split[1];

        }
        block.add(readingText);

        block.remove(0);
        readingText = "";
        //对题目处理
        for (String s1 : block) {
//                   先将题目与材料分离，将题目根据\n分割，然后判断：如果这一行包含特定格式的如（*）为（1），否为上一题的子题
            String material = null;

            List<String> topics = new ArrayList<>();
            //将题目匹配出来
            Matcher matcher3 = Pattern.compile("\\n(\\d\\d\\.[\\s\\S]*?A\\.[\\s\\S]*?B\\.[\\s\\S]*?C\\.[\\s\\S]*?D.*)").matcher(s1);
            while (matcher3.find()) {
                //格式化
                String topic = matcher3.group(1);
                s1 = s1.replace(topic, "");
                while (topic.contains("\t\t")) {
                    topic = topic.replace("\t\t", "\t");
                }
                topic = topic.replace("\t", "\n");
                topic = topic.replace(". ", ".");
                topic = topic.replace("\n\n", "\n");

                topics.add(topic + "\n" + suffix);
            }
            material = s1;
            //对材料添加段落标签 ！
            material = FormatToReplace.paragraphTags(material);

            s1 = "";
            for (String topic : topics) {
                s1 = s1 + topic;
            }

            s1 = pre + material + pre_c + s1;
            readingText = readingText + s1;
        }
        return readingText;
    }

    public static String clozeEnglilsh(String clozeText) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.MATERIAL.22&gt;Cloze\n" +
                "&lt;材料&gt;";
        String pre_c = "\n&lt;TYPE.SINGLE.22&gt;\n\n";
        String suffix = "【答案】A\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        String material = null;
        List<String> topics = new ArrayList<>();
        //匹配材料
        Matcher matcher1 = Pattern.compile("(Directions:.*Answer Sheet.)([\\s\\S]*?)\\d\\d\\.[\\s\\S]*?A\\.").matcher(clozeText);
        while (matcher1.find()) {
            // 对材料添加段落标签 ！
            material = matcher1.group(2);
            material = FormatToReplace.paragraphTags(material);
            clozeText = clozeText.replace(matcher1.group(1), "");
            clozeText = "\n"+clozeText.replace(matcher1.group(2), "");
            String topic;
            Matcher matcher3 = Pattern.compile("\\n(\\d\\d\\.[\\s\\S]*?A\\.[\\s\\S]*?B\\.[\\s\\S]*?C\\.[\\s\\S]*?D.*)").matcher(clozeText);
            while (matcher3.find()) {
                topic = matcher3.group(1);
                while (topic.contains("\t\t")) {
                    topic = topic.replace("\t\t", "\t");
                }
                topic = topic.replace("\t", "\n");
                topic = topic.replace(". ", ".");
                Matcher matcher = Pattern.compile("(\\d\\d).").matcher(topic);
                while (matcher.find()) {
                    topic = topic.replace(matcher.group(), matcher.group(1) + "请选择最合适的词语填在对应空格上\n");
                }
                topics.add(topic);
            }
        }
        clozeText = pre + material+pre_c;
        for (String topic : topics) {
            clozeText = clozeText + topic + "\n" + suffix;
        }

        return clozeText;
    }

    public static String writeEnglish(String writeText) {
        String pre = "\n" +
                "&lt;block&gt;\n" +
                "&lt;TYPE.SUBJECT.23&gt;Writing\n\n";
        String suffix = "【答案】这是主观题题的答案\n" +
                "【解析】这是主观题题的解析\n" +
                "【分值】5\n\n";
        writeText= writeText.replaceAll("Direction.*?\n\\d\\d\\.", "");
        writeText=FormatToReplace.paragraphTags(writeText);
        writeText = pre + writeText + suffix;
        return writeText;
    }

    public static String getTitle(String text,String num,String minute) {
        String titleName = "";
        String sujectRegex = "语文|数学|英语|管理学|政治";
        String year = null;
        String subject = null;
        Matcher matcher = Pattern.compile("&lt;center&gt;(.*?)&lt;/center&gt;").matcher(text);
        List<String> list = new ArrayList<>();
        while (matcher.find()) {
            titleName = titleName + matcher.group(1);
            list.add(matcher.group(1));
        }
        //格式化试卷名
        titleName= titleName.replace(" ", "");
        Matcher matcher1 = Pattern.compile("\\d{4}").matcher(list.get(0));
        while (matcher1.find()) {
            year = matcher1.group();
            titleName= titleName.replace(matcher1.group(), " " + matcher1.group() + " ");
        }
        Matcher matcher2 = Pattern.compile(sujectRegex).matcher(list.get(1));
        while (matcher2.find()) {
            subject = matcher2.group();
        }

        String pre = "&lt;block&gt;\n" +
                "&lt;TYPE.TAG&gt;文本行\n" +
                "【试卷名称】" + titleName + "\n" +
                "【试卷总分】" + num + "\n" +
                "【试卷限时(分钟)】" + minute + "\n" +
                "【试卷类型】1\n" +
                "【试卷年份】" + year + "\n" +
                "【科目】" + subject + "\n" +
                "【试卷说明】\n";
        return pre;
    }
}
